package com.cx.redis.config;

import com.cx.util.SerializeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;


/**
 * 自定义Redis序列化
 * Created by cx on 2017-09-23.
 */
public class EntityRedisSerializer implements RedisSerializer {
    private static final Logger logger=LoggerFactory.getLogger(EntityRedisSerializer.class);

    @Override
    public byte[] serialize(Object o) throws SerializationException {
        if (o == null) {
            return new byte[0];
        }
        try {
            return SerializeUtil.serialize(o);
        } catch (Exception e) {
            logger.error("序列化错误",e);
            return null;
        }
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        try {
            return SerializeUtil.unserialize(bytes);
        } catch (Exception e) {
            logger.error("反序列化错误",e);
            return null;
        }
    }
}
